<?php
/**
 * Contact Model
 *
 * @property ContactGroup
 */
class ContactGroup extends AppModel {
/**
 * Display field
 *
 * @var string
 */
	public $displayField = 'name';

    /**
     * associations
     *
     * @var array
    **/

    var $hasMany = array(
        'Contact' => array(
            'className' => 'Contact',
            'foreignKey' => 'contact_group_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    var $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'order' => '',
            'limit' => '',
            'dependent' => false
        )
    );
    
    
    function getContactGroupById($id = null){
        $conditions = array(
           'ContactGroup.id' => $id,
           'ContactGroup.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return  $this->find('first', array('conditions' => $conditions, 'recursive' => -1));
    }

    function getContactGroupOptions($user_id = null){
        $conditions = array(
            'ContactGroup.user_id' => $user_id,
            'ContactGroup.deleted' => 'n'
        );
        # fetch the specific data from the server and retrun it.
        return  $this->find('list', array('fields'=>array('ContactGroup.id','ContactGroup.name'),'conditions' => $conditions, 'recursive' => -1));
    }


    function deleteContactGroup($ids = null, $user_id){
        //first remove all contacts from this groups
        $this->Contact->updateAll(
            array('Contact.contact_group_id' =>0),
            array('Contact.contact_group_id' => $ids,'Contact.user_id' => $user_id)
        );

        $result = $this->updateAll(
            array('ContactGroup.deleted' => "'y'"),
            array('ContactGroup.id' => $ids,'ContactGroup.user_id' => $user_id)
        );
       return $result;
    }


    function countContactsInGroup($user_id = null){
        $data = $this->find('all', array(
            'conditions' => array('ContactGroup.user_id' => $user_id,'ContactGroup.deleted' => 'n'),
            'fields' => array('ContactGroup.id', 'ContactGroup.name'),
            'contain' => array('Contact'=>array('fields'=>array('Contact.id')))
        ));

        $res = array();
        foreach($data as $dt){
            $group_id = $dt['ContactGroup']['id'];
            $group_count = count($dt['Contact']);
            $group_name  = $dt['ContactGroup']['name'];
            $res[$group_id] = array(
                'name'=>$group_name,
                'count'=>$group_count
            );
        }
        //Count for No Group Also
         $no_group_count = $this->Contact->find('count', array(
             'conditions' => array('Contact.user_id' => $user_id,'Contact.contact_group_id' => 0,'Contact.deleted' => 'n'),
             'recursive' => -1
         ));

        $res[0]= array(
            'name'=>'No Group',
            'count'=>$no_group_count
        );

        return $res;
    }


    function getTotal($user_id = null){
        $data = $this->find('count', array(
            'conditions' => array('ContactGroup.user_id' => $user_id,'ContactGroup.deleted' => 'n'),
            'recursive' => -1
        ));
        return $data;
    }
   
}
